package com.akupilih.app;

import java.io.File;
import java.util.HashMap;

import org.acra.ACRA;
import org.acra.ReportField;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;

import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.utils.StorageUtils;

/**
 * Base Application Class
 * 
 * @author hafizridha.net
 */

@ReportsCrashes(formKey = "", mailTo = ACRAPostSender.EMAIL_REPORTING, customReportContent = {
		ReportField.ANDROID_VERSION, ReportField.APP_VERSION_CODE,
		ReportField.PHONE_MODEL, ReportField.CUSTOM_DATA,
		ReportField.STACK_TRACE, ReportField.LOGCAT }, mode = ReportingInteractionMode.SILENT)
public class BaseApplication extends Application {

	// set default timeout
	// if server not response after timeout, retry
	private static final int TIMEOUT_MS = 25000; // 5second

	private RequestQueue requestQueue;
	private static BaseApplication instance;

	@Override
	public void onCreate() {
		super.onCreate();

		File cacheDir = StorageUtils.getCacheDirectory(getApplicationContext());

		int availableMemory = (int) Runtime.getRuntime().maxMemory() / 1048576;

		ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
				getApplicationContext())
				.discCacheExtraOptions(480, 800, CompressFormat.PNG, 75, null)
				.discCache(new UnlimitedDiscCache(cacheDir))
				.discCacheFileCount(50).writeDebugLogs()
				.memoryCache(new WeakMemoryCache())
				.memoryCacheSize((availableMemory / 2) * 1024 * 1024)
				.threadPoolSize(1).build();

		ImageLoader.getInstance().init(configuration);

		instance = this;

		ACRAPostSender.TRACE("BaseApplication", "onCreate : data kosong");

		ACRA.init(this);
		HashMap<String, String> ACRAdata = new HashMap<String, String>();
		ACRAdata.put("my_app_info", getApplicationContext().getPackageName()
				.toString());
		ACRA.getErrorReporter().setReportSender(new ACRAPostSender(ACRAdata));
	}

	public static synchronized BaseApplication getInstance() {
		return instance;
	}

	/**
	 * Get RequestQueue
	 * 
	 * @return
	 */
	public RequestQueue getRequestQueue() {
		ACRAPostSender.TRACE("BaseApplication", "getRequestQueue : ");
		if (requestQueue == null) {
			ACRAPostSender.TRACE("BaseApplication",
					"getRequestQueue : make new instance ");
			requestQueue = Volley.newRequestQueue(getApplicationContext());
		}
		return requestQueue;
	}

	/**
	 * add to request using tag
	 * 
	 * @param request
	 * @param tag
	 */
	public <T> void addToRequestQueue(Request<T> request, String tag) {
		request.setTag(tag);

		// set retry policy
		request.setRetryPolicy(new DefaultRetryPolicy(TIMEOUT_MS,
				DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
				DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

		ACRAPostSender.TRACE("BaseApplication", "addToRequestQueue : "
				+ request.getUrl());
		getRequestQueue().add(request);
	}

	public void cancelPendingRequest(Object tag) {
		if (requestQueue != null)
			requestQueue.cancelAll(tag);
	}

	public static DisplayImageOptions getDisplayImageOptions() {
		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.cacheOnDisc(true).imageScaleType(ImageScaleType.IN_SAMPLE_INT)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		return options;
	}
}
